package com.powernode.controller;

/**
 * @version 1.0
 * @Date 2024/8/15 16:47
 * @Author 付~~~
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysUserService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理员控制层
 */
@Api(tags = "系统管理员接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询登录的用户信息
     * @return
     */
        @ApiOperation("查询登录的用户信息")
        @GetMapping("info")
        public Result<SysUser> loadSysUserInfo(){
            // 获取登录用户标识
            Long userId = AuthUtils.getLoginUserId();
            // 根据用户标识查询登录用户信息
            SysUser user = sysUserService.getById(userId);

            return Result.success(user);
        }

    /**
     * 分页查询管理员列表
     * @param current 当前页
     * @param size 每页显示条数
     * @param username 用户名。可有可无
     * @return
     */
    @ApiOperation("多条件分页查询系统管理员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")     //登录进来后，需要请求资源，需要验证当前用户是否有权限去验证这些资源，故而添加该注解判断它所拥有得权限
    public Result<Page<SysUser>> loadSysUserPage(@ApiParam("页码") @RequestParam Long current,
                                                 @ApiParam("每页显示数") @RequestParam Long size,
                                                 @ApiParam("用户名称") @RequestParam(required = false) String username){
        //创建一个Mybatisplus分页对象
        Page<SysUser> page = new Page<>(current,size);
        //根据多条件分页查询系统管理员
        page = sysUserService.page(page,new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(username),SysUser::getUsername,username)
                .orderByDesc(SysUser::getUsername)
        );
        return Result.success(page);
    }

    /**
     * 新增管理员
     * @param sysUser 管理员对象
     * @return
     */
    @ApiOperation("新增管理员")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:save')")  //权限新增
    public Result<String> saveSysUser(@RequestBody SysUser sysUser){
        Integer count = sysUserService.saveSysUser(sysUser);
        return Result.handle(count > 0);
    }

    /**
     * 根据标识查询系统管理员信息
     * @param id 管理员编号
     * @return
     */
    @ApiOperation("根据标识查询系统管理员信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserInfo(@PathVariable Long id){
        SysUser sysUser = sysUserService.querySysUerInfoByUserId(id);
        return Result.success(sysUser);
    }

    /**
     * 修改管理员信息
     * @param sysUser 管理员信息
     * @return
     */
    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUserInfo(@RequestBody SysUser sysUser){
        Integer count = sysUserService.modifySysUser(sysUser);
        return Result.handle(count>0);
    }

    /**
     * 批量或者单个删除管理信息
     * @param userIds 管理员标识集合id
     * @return
     */
    @ApiOperation("批量或者单个删除管理信息")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUsers(@PathVariable List<Long> userIds){
        Boolean removed = sysUserService.removeSysUserListByUserIds(userIds);
        return Result.handle(removed);
    }

}
